package com.atao.baokede.mapper;

import com.atao.baokede.pojo.Cart;
import com.atao.baokede.pojo.DetailOrder;
import com.atao.baokede.pojo.Item;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

@Mapper
public interface ItemMapper {
    // 根据id查询商品
    @Select("select * from item where id = #{id}")
    Item findById(Long id);

    //根据商家id查询商品
    @Select("select * from item where merchant_id = #{merchantId}")
    List<Item> findItemsByMerchantId(Long merchantId);

    // 添加商品
    @Select("insert into item (merchant_id, name, price, stock, image, category,is_user_add_category, brand, spec,sold,comment_count, status, create_time, update_time)" +
            "values(#{merchantId},#{name},#{price},#{stock},#{image},#{category},#{isUserAddCategory},#{brand},#{spec},#{sold},#{commnetCount},#{status},now(),now())")
    void addItem(Item item);

    // 查询所有上架商品
    @Select("select * from item where status = #{status}")
    List<Item> findAllActiveItems(Integer status);

    // 查询所有待审核商品
    @Select("select * from item where status = #{status}")
    List<Item> findAllPendingItems(Integer status);

    // 查询所有首页商品
    @Select("select * from item where status = #{status}")
    List<Item> findHomeItems(int statusOnSale);

    // 扣库存
    @Select("update item set stock = stock - #{num} where id = #{itemId}")
    void reduceStock(Long itemId, Long num);

    // 批量扣库存
    void reduceStockBatch(@Param("cartList") List<Cart> cartList);

    // 添加库存
    @Update("update item set stock = stock + #{num} where id = #{itemId}")
    void addStock(DetailOrder detailOrder);

    // 批量添加库存
    void addStocks(@Param("detailOrders") List<DetailOrder> detailOrders);

    // 禁用商户的商品
    @Update("update item set status = 2 where merchant_id = #{merchantId}")
    void disableItemsByMerchantId(Long merchantId);

    // 启用商户的商品
    @Update("update item set status = 2 where merchant_id = #{merchantId}")
    void enableItemsByMerchantId(Long merchantId);

    // 禁用商品
    @Update("update item set status = 2 where id = #{itemId}")
    void disableItem(Long itemId);

    // 修改商品信息
    @Update("update item set name = #{name}, price = #{price}, stock = #{stock}, image = #{image}, category = #{category}, brand = #{brand}, spec = #{spec},status = 0, update_time = now() where id = #{id}")
    void updateItem(Item item);

    // 删除商品
    @Delete("delete from item where id = #{itemId}")
    void deleteItem(Long itemId);

    // 批量删除商品
    void deleteItems(@Param("itemIds") List<Long> itemIds);

    // 批量下架商品
    void disableItems(@Param("itemIds") List<Long> itemIds);

    // 根据商家id查询商品id
    @Select("select id from item where merchant_id = #{merchantId}")
    List<Long> getItemsIdsByMerchantId(Long merchantId);

    // 获取最畅销商品
    @Select("select name , price , category , brand , spec ,sold , comment_count from item " +
            "where status = 1 order by sold desc ,comment_count desc ,sold desc limit 20")
    List<Item> getBestSellingItems();

    // 启用商品
    @Update("update item set status = 0 where id = #{itemId}")
    void enableItem(Long itemId);

    // 批量启用商品
    void enableItems(@Param("itemIds") List<Long> itemIds);

    // 根据商品id批量查询商品
    List<Item> findItemsByIds(List<Long> itemIds);

    // 根据商品id批量查询商品名称（返回多行）
    List<Map<String, Object>> findItemNamesByIds(List<Long> itemIds);

    // 根据商品id批量查询商品图片（返回多行）
    List<Map<String, Object>> findImagesByIds(List<Long> itemIds);
}
